<?php 
class UsersModel extends Model
{
	protected $_table = 'users';
	protected $_idField = 'user_id';
	public $properties = array('user_loginname', 'user_password', 'group_id', 'user_nicename', 
				'user_email', 'dt_registered', 'ip_last_login', 'dt_last_login', 'user_status');
	
	public $rules = array(
			'user_loginname' => array(
				'required' => true,	
				'minlength' => 6, 
				'maxlength' => 12, 
				'unique' => 'user_loginname', 
				'when' => MODEL::INSERT
			),
			'user_password' => array(
				'required' => true,	
				'minlength' => 6, 
				'maxlength' => 16, 
				'when' => MODEL::BOTH
			),
			'user_repassword' => array(
				'required' => true, 
				'equalTo' => 'user_password', 
				'when' => MODEL::BOTH
			),
			'user_email' => array(
				'required' => true, 
				'email' => true, 
				'when' => MODEL::BOTH
			)
		);

	public $messages = array(
			'user_loginname' => array(
				'required' => '请填写登录名',
				'minlength' => '登录名长度由大于6个英文字母、数字、.或_组成',
				'maxlength' => '登录名长度由小于12个英文字母、数字、.或_组成',
				'unique' => '此登录名已被使用，请更换登录名'
			),
			'user_password' => array(
				'required' => '请填写密码',
				'minlength' => '密码长度由大于6个英文字母、数字、或特殊符号组成',
				'maxlength' => '密码长度由小于16个英文字母、数字、或特殊符号组成'
			),
			'user_repassword' => array(
				'required' => '请再次输入密码',
				'equalTo' => '两次密码输入不一致'
			),
			'user_email' => array(
				'required' => '请填写邮箱，可以通过邮箱找回密码',
				'email' => '邮箱格式不正确'
			)
		);
	
	public function preInsert()
	{
		$this->ip_last_login = Helper::getIp();
		$this->dt_last_login = $this->dt_registered = date('Y-m-d H:i:s');
	}
	
	public function preAutoSave()
	{
		if (!(isset($_REQUEST['user_status']) && 
			  in_array($_REQUEST['user_status'], array('y', 'n'))))
			$_REQUEST['user_status'] = 'y';
		
		$_REQUEST['group_id'] = isset($_REQUEST['group_id']) ? (int) $_REQUEST['group_id'] : '';
	}
}
